package io.persimmon.core.dto;

import io.persimmon.core.exception.PersimmonExceptionFactory;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

@Data
@ApiModel("接口响应信息")
public class Response<T> {
    @ApiModelProperty("响应码")
    private String responseCode;
    @ApiModelProperty("响应信息")
    private String responseMessage;
    @ApiModelProperty("提示信息")
    private String alertMessage;
    @ApiModelProperty("异常id")
    private String exceptionId;
    @ApiModelProperty("响应数据")
    private T data;

    public boolean isSuccess() {
        return ResponseCode.SUCCESS.getCode().equals(this.responseCode);
    }

    public T getDataWithCheck() {
        if (!this.isSuccess()) {
            throw PersimmonExceptionFactory.create(this.getResponseMessage())
                    .responseCode(ResponseCode.codeOf(this.getResponseCode()))
                    .alertMessage(this.getAlertMessage())
                    .exceptionId(this.exceptionId);
        }
        return this.data;
    }
}
